﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Aspfm.Html
{
    class TestHttpListener
    {
        static System.Net.HttpListener sSocket = null;

        static void Start()
        {
            sSocket = new System.Net.HttpListener();
            sSocket.Prefixes.Add("http://127.0.0.1:8081/");
            sSocket.Start();
            sSocket.BeginGetContext(new AsyncCallback(GetContextCallBack), sSocket);         
        }
        static void GetContextCallBack(IAsyncResult ar)
        {
            try
            {
                sSocket = ar.AsyncState as System.Net.HttpListener;
                System.Net.HttpListenerContext context = sSocket.EndGetContext(ar);
                sSocket.BeginGetContext(new AsyncCallback(GetContextCallBack), sSocket);
                string t = context.Request.QueryString["t"];
                int i = 0;
                int.TryParse(t,out i);
                System.Threading.Thread.Sleep(i*1000);

                Console.WriteLine(context.Request.Url.PathAndQuery+"      "+t);

                byte[] buffer = System.Text.Encoding.UTF8.GetBytes("hhh:"+t);  
                var output = context.Response.OutputStream;
                output.Write(buffer, 0, buffer.Length);
                output.Close();  
            }
            catch { }

        }
    }
}
